import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const bilStore = createSlice({
  name: 'bliStore',
  initialState: {
    billList: []
  },
  reducers: {
    setBilList: (state, action) => {
      state.billList = action.payload;
    },
    // 添加账单
    addBill: (state, action) => {
      state.billList.push(action.payload);
    }
  }
});

const { setBilList, addBill } = bilStore.actions;
const bilReducer = bilStore.reducer
// 编写异步
const getBillList = () => {
  return async (dispatch) => {
    const res = await axios.get('http://localhost:8888/ka')
    dispatch(setBilList(res.data))
  }
}
// 添加数据
const addBillList = (data) => {
  return async (dispatch) => {
    const res = await axios.post('http://localhost:8888/ka', data)
    dispatch(addBill(res.data))
  }
}
export { getBillList, addBillList }
export default bilReducer